<template>
    <div>
        <!--搜索框-->
        <div class="search-form">
            <el-form :inline="true" :model="searchForm" class="demo-form-inline">
                <el-form-item label="账户名">
                    <el-input v-model="searchForm.name" placeholder="请输入昵称"></el-input>
                </el-form-item>
                <el-form-item label="用户状态">
                    <el-select v-model="searchForm.available" placeholder="请选择用户状态">
                        <el-option label="正常" :value="1"></el-option>
                        <el-option label="禁用" :value="0"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="searchSubmit">查询</el-button>
                    <el-button type="primary" @click="resetSearchForm">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

        <!--头部工具栏-->
        <div class="header-toolbox">
            <el-button type="primary" size="small" @click="addOrUpdateUser">增加</el-button>
            <el-button type="danger" size="small" @click="batchDelete">批量删除</el-button>
        </div>

        <!--表格数据-->
        <div class="data-table">
            <el-table
                    :data="tableData"
                    @selection-change="handleSelectionChange"
                    style="width: 100%">
                <el-table-column
                        type="selection"
                        width="55px">
                </el-table-column>
                <el-table-column
                        label="账号名">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.name }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        label="登陆名">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.loginname }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        label="地址">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.address }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        label="性别">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.sex === 1 ? '男' : '女' }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        label="角色">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.remark }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        label="部门名称">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.deptName }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        label="入职时间">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.hiredate }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        label="账号状态">
                    <template slot-scope="scope">
                        <el-switch
                                v-model="scope.row.available"
                                :active-value="1"
                                :inactive-value="0"
                                active-color="#13ce66"
                                inactive-color="#ff4949"
                                @change="change(scope.row)">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="150px">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                @click="handleEdit(scope.$index, scope.row)">编辑
                        </el-button>
                        <el-button
                                size="mini"
                                type="danger"
                                @click="handleDelete(scope.$index, scope.row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <div class="foot-page">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="searchForm.currentPage"
                    :page-sizes="[5, 10, 15, 20]"
                    :page-size="searchForm.pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total"
                    class="foot-pageInfo">
            </el-pagination>
        </div>

        <el-dialog :title="title" :visible.sync="userFormVisible" @close="closeUserForm">
            <el-form :model="userForm" :rules="userFormRules" ref="userFormRef" label-width="80px"
                     label-position="left">
                <el-form-item label="账号名" prop="name">
                    <el-input v-model="userForm.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="登陆名" prop="loginname">
                    <el-input v-model="userForm.loginname" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="性别" prop="sex">
                    <el-select v-model="userForm.sex" placeholder="请选择性别" style="width: 100%;">
                        <el-option label="男" :value="1"></el-option>
                        <el-option label="女" :value="0"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="城市" prop="address">
                    <el-input v-model="userForm.address" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="部门" prop="deptid">
                    <el-select v-model="userForm.deptid" placeholder="请选择部门" style="width: 100%;">
                        <el-option :label="dept.name" :value="dept.id" :key="index"
                                   v-for="(dept, index) in depts"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="角色" prop="remark">
                    <el-select v-model="userForm.remark" placeholder="请选择角色"
                               style="width: 100%">
                        <el-option v-for="(role,index) in roles" :key="index" :label="role.name"
                                   :value="role.name"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="入职时间" prop="hiredate">
                    <el-date-picker v-model="userForm.hiredate" autocomplete="off"></el-date-picker>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer" align="center">
                <el-button @click="userFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitUser">确 定</el-button>
            </div>
        </el-dialog>

    </div>

</template>

<script>
import {
    addAndUpdateUserAPI,
    batchDeleteUserAPI, changeUserAvailableAPI, getCurrentPageUserInfoAPI,
    getDeptsListAPI,
    getFullUserInfoAPI, getRoleListAPI,
} from '@/request/api'

export default {
    name: "UserView",
    data() {
        return {
            title: '',
            total: 100,
            userFormVisible: false,
            searchForm: {
                currentPage: 1,
                pageSize: 5,
            },
            tableData: [],
            multipleSelection: [],
            depts: [],
            roles: [],
            userForm: {},
            userFormRules: {
                name: [
                    {required: true, message: '请填写账号名', trigger: 'blur'}
                ],
                loginname: [
                    {required: true, message: '请填写登陆名', trigger: 'blur'}
                ],
                sex: [
                    {required: true, message: '请选择性别', trigger: 'blur'}
                ],
                address: [
                    {required: true, message: '请填写城市', trigger: 'blur'}
                ],
                deptName: [
                    {required: true, message: '请选择部门', trigger: 'blur'}
                ],
                role: [
                    {required: true, message: '请选择权限', trigger: 'blur'}
                ],
                hiredate: [
                    {required: true, message: '请选择入职时间', trigger: 'blur'}
                ]
            }
        }
    },
    methods: {
        searchSubmit() {
            this.searchForm.pageSize = 5,
                this.searchForm.currentPage = 1
            this.list()
        },
        resetSearchForm() {
            this.searchForm = {
                currentPage: this.searchForm.currentPage,
                pageSize: this.searchForm.pageSize,
            }
        },
        batchDelete() {
            this.$confirm('确认执行删除操作么, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                batchDeleteUserAPI(this.multipleSelection).then(response => {
                    if (response.code == 200) {
                        this.$message.success({
                            type: 'success',
                            message: response.msg,
                            onClose: () => {
                                this.searchForm = {
                                    currentPage: 1,
                                    pageSize: 5,
                                }
                                this.list()
                            },
                        })
                        return
                    }
                    this.$message.error(response.msg)
                }).catch(error => {
                    this.$message.error('删除操作错误')
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        // 用户编辑
        handleEdit(index, row) {
            this.title = '编辑表单';
            this.userFormVisible = true;
            getFullUserInfoAPI(row.id).then(response => {
                if (response.code == 200) {
                    this.userForm = response.data;
                }
            }).catch(error => {
                this.$message.error('请求失败')
            })
        },
        // 用户删除
        handleDelete(index, row) {
            this.$confirm('确认执行删除操作么, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.multipleSelection = [row.id]
                batchDeleteUserAPI(this.multipleSelection).then(response => {
                    if (response.code == 200) {
                        this.$message.success({
                            type: 'success',
                            message: response.msg,
                            onClose: () => {
                                this.searchForm = {
                                    currentPage: 1,
                                    pageSize: 5,
                                }
                                this.list()
                            },
                        })
                        return
                    }
                    this.$message.error(response.msg)
                }).catch(error => {
                    this.$message.error('删除操作错误')
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        // 开启复选框
        handleSelectionChange(val) {
            this.multipleSelection = val.map(row => row.id)
        },
        // 页大小改变
        handleSizeChange(val) {
            this.searchForm.pageSize = val
            // this.$message.info(this.searchForm.pageSize)
            this.list()
        },
        // 当前页改变
        handleCurrentChange(val) {
            this.searchForm.currentPage = val
            // this.$message.info( this.searchForm.currentPage)
            this.list()
        },
        // 改变账户状态
        change(row) {
            changeUserAvailableAPI({id: row.id, available: row.available}).then(response => {
                // 进行判断 注入值
                if (response.code == 200) {
                    this.$message.success({
                        type: 'success',
                        message: response.msg,
                        onClose: () => {
                            this.list()
                        }
                    })
                    return
                }
                this.$message.error(response.msg)
            }).catch(error => {
                this.$message.error('改变状态错误')
            })
        },
        list() {
            getCurrentPageUserInfoAPI(this.searchForm).then(response => {
                // 进行判断 注入值
                if (response.code == 200) {
                    this.tableData = response.data.data;
                    console.log(this.tableData)
                    this.total = response.data.count;
                    return;
                }
                this.$message.error(response.msg)
            }).catch(error => {
                this.$message.error("操作失败")
            })
        },
        // 打开用户用户表单
        addOrUpdateUser() {
            this.title = '新增表单'
            this.userFormVisible = !this.userFormVisible;
        },
        // 把添加用户和更新用户请求封成一个方法
        addOrUpdateUserBody() {
            addAndUpdateUserAPI(this.userForm).then(response => {
                if (response.code == 200) {
                    this.$message.success({
                        type: 'success',
                        message: response.msg,
                        onClose: () => {
                            this.list()
                            this.userFormVisible = false;
                            this.userForm = {}
                        }
                    })
                    return;
                }
                this.$message.error(response.msg)
            }).catch(error => {
                this.$message.error("用户操作失败")
            })
        },
        // 用户表单增加或修改
        submitUser() {
            this.addOrUpdateUserBody()
        },
        // 清除意外关闭表单的数据
        closeUserForm() {
            this.userForm = {}
            this.$refs.userFormRef.clearValidate()
        },
        renderSelect() {
            getRoleListAPI().then(response => {
                this.roles = response.data
            })
            getDeptsListAPI().then(response => {
                this.depts = response.data
                return
            })
        }
    },
    created() {
        this.list()
        this.renderSelect()
    }
}
</script>

<style scoped>
.demo-form-inline {
    float: left;
    padding: 15px 0px 5px 10px;

}

.header-toolbox {
    width: 180px;
    padding-bottom: 10px;
}

.foot-pageInfo {
    float: left;
}
</style>